package com.optitronics.util;

import java.util.Properties;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;

public class OptitronicsStringEditor extends StringTrimmerEditor {

	private Properties charsToReplace;

	public OptitronicsStringEditor(boolean emptyAsNull) {
		super(emptyAsNull);
	}

	public OptitronicsStringEditor(String charsToDelete, boolean emptyAsNull) {
		super(charsToDelete, emptyAsNull);
	}

	public OptitronicsStringEditor(boolean emptyAsNull, Properties charsToReplace) {
		super(emptyAsNull);
		this.charsToReplace = charsToReplace;
	}

	@Override
	public void setAsText(String text) {

		String transformedText = text;

		for (Object objKey : charsToReplace.keySet()) {

			String key = objKey.toString();
			String value = charsToReplace.get(key).toString();
			transformedText = transformedText.replaceAll(key, value);
			transformedText = transformedText.replaceAll(key.toUpperCase(), value.toUpperCase());
		}

		super.setAsText(transformedText);
	}
}
